<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="disabled">
      <a-form-model layout="horizontal" ref="subForm" slot="detail" :model="model" :rules="validatorRules" v-bind="formItemLayout">
        <z-form-item :datas="fieldArr" :form="model" :rules="validatorRules" :gutter="0" @change="getChange" />
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { getMstPersonCount, allClinicInfo, mstPersonJoinApply } from '@/api/sjsk/personService'
  export default {
    props: {
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model: {},
        confirmLoading: false,
        formItemLayout: {
          labelCol: { span: 6 },
          wrapperCol: { span: 16 }
        },
        dictOptions: [],
        fieldArr: [
          {label: '姓名', prop: 'realName', tagType: 'input', disabled: true},
          {label: '身份证号', prop: 'identityCard', tagType: 'input', disabled: true},
          {label: '美沙酮治疗门诊', prop: 'clinicId', tagType: 'dict-select', dictOptions: []},
          {label: '门诊所在地区', prop: 'clinicRegion', tagType: 'input', disabled: true},
          {label: '检测材料', prop: 'testingMaterials', tagType: 'upload-file', fileFormat: '.doc,.docx,.xls,.xlsx,.pdf,.jpg,.jpeg,.png'},
        ],
        validatorRules: {
          clinicId:[ { required: true, message: '请选择美沙酮治疗门诊' },],
          testingMaterials:[ { required: true, message: '请选择检测材料' },],
        }
      }
    },
    methods: {
      show (record) {
        // this.model = record?Object.assign({}, record):this.model;
        this.$set( this.model, 'sources', 1 )
        this.$set( this.model, 'personId', record.id )
        this.$set( this.model, 'realName', record.realName )
        this.$set( this.model, 'identityCard', record.identityCard )
        this.$set( this.model, 'phone', record.phone )
        this.$set( this.model, 'sex', record.sex )
        this.$set( this.model, 'birthday', record.birthday )
        this.$set( this.model, 'nation', record.nation )
        this.$set( this.model, 'marriage', record.marriage )
        this.$set( this.model, 'education', record.education )
        this.$set( this.model, 'occupation', record.occupation )
        this.$set( this.model, 'firstDrugTime', record.firstDrugTime )
        this.$set( this.model, 'drugAbuseMode', record.drugWays )
        this.$set( this.model, 'drugType', record.drugType )
        this.$set( this.model, 'currentTown', record.currentTown )
        this.$set( this.model, 'currentAddress', record.currentAddress )
        this.$set( this.model, 'groupCount', 0 )
        this.getAllClinicInfo()
        if(record.caseNo) this.getMstPersonCount(record.caseNo)
      },
      submitForm () {
        const parameter = Object.assign({}, this.model)
        this.$refs.subForm.validate((valid)=> {
          if (valid) {
            mstPersonJoinApply(parameter)
              .then(res => {
                this.confirmLoading = false
                if (res.success) {
                  this.$message.success(res.message)
                  this.$emit('ok');
                } else {
                  this.$message.warn(res.message)
                }
              })
              .catch(e => {
                this.$message.warn('请求失败！')
                this.confirmLoading = false
              })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      // 获取美沙酮治疗门诊
      getAllClinicInfo() {
        allClinicInfo()
          .then(res => {
            if (res.success) {
              const { result } = res
              let results = result.map((v,i)=> {
                return { text: v.clinicName, value: v.clinicId?v.clinicId:i, region: v.clinicRegion }
              })
              this.dictOptions = results
              this.fieldArr.forEach((item,index) => {
                let d = [{ text:"请选择", value:"" }]
                if(item.prop==='clinicId') {
                  item.dictOptions = [...d, ...this.dictOptions]
                }
              })
            } else {
              this.$message.warn(res.message)
            }
          })
          .catch(e => {
            this.$message.warn('请求失败！')
          })
      },
      // 获取人员入组次数
      getMstPersonCount(caseNo) {
        getMstPersonCount({caseNo: caseNo})
          .then(res => {
            if (res.success) {
              const { result } = res
              this.$set( this.model, 'groupCount', result? result:0 )
            } else {
              this.$message.warn(res.message)
            }
          })
          .catch(e => {
            this.$message.warn('请求失败！')
          })
      },
      getChange(prop, value) {
        if(prop === 'clinicId') {
          this.$set( this.model, 'clinicName', this.$options.filters.filterArray(value, this.dictOptions, 'value', 'text') )
          this.$set( this.model, 'clinicRegion', this.$options.filters.filterArray(value, this.dictOptions, 'value', 'region') )
        }
      }
    }
  }
</script>
